const state = {
    count: 0
}

// 像计算属性一样，getter 的返回值会根据它的依赖被缓存起来，且只有当它的依赖值发生了改变才会被重新计算
const getters = {
    doubleCount(state) {
        return state.count * 2
    }
}

// 改变state中元素的数据
const mutations = {
    increment(state, n) {
        state.count += n
    },
    decrement(state) {
        state.count--
    }
}

// 异步提交mutations    
const actions = {
    INCREMENT({ commit }, n) {
        commit('increment', n)
    },
    DECREMENT({ commit }) {
        commit('decrement')
    },
    ASYN_INCREMENT({ commit }, n) {
        setTimeout(() => {
            commit('increment', n)
        }, 2000)
    },
}

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}